package cn.yunyichina.provider.framework.data;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Created by Lullaby on 2016/8/22.
 */
@Component
public class SpringContextHandler implements ApplicationContextAware {

    private static final Logger logger = LoggerFactory.getLogger(SpringContextHandler.class);

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHandler.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static <T> T getBean(String name) {
        try {
            return (T) applicationContext.getBean(name);
        } catch (NoSuchBeanDefinitionException e) {
            logger.error("SpringContextHandler.getBean(name) -> {}", name);
            return null;
        }
    }

    public static <T> T getBean(Class<T> clazz) {
        try {
            return applicationContext.getBean(clazz);
        } catch (NoSuchBeanDefinitionException e) {
            logger.error("SpringContextHandler.getBean(clazz) -> {}", clazz.getName());
            return null;
        }
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        try {
            return applicationContext.getBean(name, clazz);
        } catch (NoSuchBeanDefinitionException e) {
            logger.error("SpringContextHandler.getBean(name, clazz) -> name:{} & clazz:{}", name, clazz.getName());
            return null;
        }
    }

}
